<h1 id="运算符">运算符</h1>
<h2 id="逻辑运算符">逻辑运算符</h2>
<table>
<thead>
<tr>
<th>运算符</th>
<th>描述</th>
<th>实例</th>
</tr>
</thead>
<tbody><tr>
<td>&#124;&#124;</td>
<td>或，一真即真</td>
<td>true &#124;&#124; false =&gt; true</td>
</tr>
<tr>
<td>&amp;&amp;</td>
<td>与，一假即假</td>
<td>false &amp;&amp; true =&gt; false</td>
</tr>
<tr>
<td>!</td>
<td>非，逻辑取反</td>
<td>!false =&gt; true</td>
</tr>
</tbody></table>
<p><code>||</code>可以用于实现默认值：</p>
<pre><code class="hljs language-javascript"><span class="hljs-comment">// 当 c 为null, false, 或相应类型默认值时， a 将被赋值 &quot;vue&quot;</span>
a = c || <span class="hljs-string">&quot;vue&quot;</span>
</code></pre>
<p><code>&amp;&amp;</code>可以用作条件判断：</p>
<pre><code class="hljs language-javascript"><span class="hljs-comment">// 当第一个函数返回null, false, 或相应类型默认值时，第二函数不会执行；反之，会执行。</span>
<span class="hljs-title function_">fn1</span>() &amp;&amp; <span class="hljs-title function_">fn2</span>()
</code></pre>
<p><code>!</code>可以用作判断空：
当obj为空时， <code>!obj</code> 表达式值为true</p>
<h2 id="三目运算符">三目运算符</h2>
<pre><code class="hljs language-javascript"><span class="hljs-title function_">println</span>(<span class="hljs-literal">true</span> ? <span class="hljs-string">&quot;blue&quot;</span> : <span class="hljs-string">&quot;red&quot;</span>)  <span class="hljs-comment">// blue</span>
<span class="hljs-title function_">println</span>(<span class="hljs-literal">false</span> ? <span class="hljs-string">&quot;kind&quot;</span> : <span class="hljs-string">&quot;dom&quot;</span>) <span class="hljs-comment">// dom</span>
</code></pre>
<h2 id="算术运算符">算术运算符</h2>
<p>以下假设A为10，B为20</p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>描述</th>
<th>实例</th>
</tr>
</thead>
<tbody><tr>
<td>+</td>
<td>加法: 相加运算符两侧的值</td>
<td>A + B 等于 30</td>
</tr>
<tr>
<td>-</td>
<td>减法: 左操作数减去右操作数</td>
<td>A – B 等于 -10</td>
</tr>
<tr>
<td>*</td>
<td>乘法: 相乘操作符两侧的值</td>
<td>A * B 等于 200</td>
</tr>
<tr>
<td>/</td>
<td>除法: 左操作数除以右操作数</td>
<td>B / A 等于 2</td>
</tr>
<tr>
<td>％</td>
<td>取余: 左操作数除以右操作数的余数</td>
<td>B % A 等于 0</td>
</tr>
<tr>
<td>++</td>
<td>自增: 操作数的值增加1（不支持前置自增）</td>
<td>A++ 等于 11</td>
</tr>
<tr>
<td>--</td>
<td>自减: 操作数的值减少1（不支持前置自增）</td>
<td>B-- 等于 19</td>
</tr>
</tbody></table>
<p>实例</p>
<pre><code class="hljs language-javascript">A = <span class="hljs-number">10</span>; B = <span class="hljs-number">20</span>
<span class="hljs-title function_">println</span>(A + B) <span class="hljs-comment">// 30</span>
<span class="hljs-title function_">println</span>(A - B) <span class="hljs-comment">// -10</span>
<span class="hljs-title function_">println</span>(A * B) <span class="hljs-comment">// 200</span>
<span class="hljs-title function_">println</span>(B / A) <span class="hljs-comment">// 2</span>
<span class="hljs-title function_">println</span>(B % A) <span class="hljs-comment">// 0</span>
<span class="hljs-title function_">println</span>(A++) <span class="hljs-comment">// 11</span>
<span class="hljs-title function_">println</span>(B--) <span class="hljs-comment">// 19</span>
</code></pre>
<h2 id="比较运算符">比较运算符</h2>
<p>以下假设变量a为10，变量b为20：</p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>描述</th>
<th>实例</th>
</tr>
</thead>
<tbody><tr>
<td>==</td>
<td>等于 - 比较对象是否相等</td>
<td>(a == b) 返回 false</td>
</tr>
<tr>
<td>!=</td>
<td>不等于 - 比较两个对象是否不相等</td>
<td>(a != b) 返回 true</td>
</tr>
<tr>
<td>&gt;</td>
<td>大于 - 返回x是否大于y</td>
<td>(a &gt; b) 返回 false</td>
</tr>
<tr>
<td>&lt;</td>
<td>小于 - 返回x是否小于y。</td>
<td>(a &lt; b) 返回 true</td>
</tr>
<tr>
<td>&gt;=</td>
<td>大于等于 - 返回x是否大于等于y。</td>
<td>(a &gt;= b) 返回 false</td>
</tr>
<tr>
<td>&lt;=</td>
<td>小于等于 - 返回x是否小于等于y。</td>
<td>(a &lt;= b) 返回 true</td>
</tr>
</tbody></table>
<p>实例</p>
<pre><code class="hljs language-javascript">a = <span class="hljs-number">10</span>; b = <span class="hljs-number">20</span>
<span class="hljs-title function_">println</span>(a == b) <span class="hljs-comment">// false</span>
<span class="hljs-title function_">println</span>(a != b) <span class="hljs-comment">// true</span>
<span class="hljs-title function_">println</span>(a &gt; b) <span class="hljs-comment">// false</span>
<span class="hljs-title function_">println</span>(a &lt; b) <span class="hljs-comment">// true</span>
<span class="hljs-title function_">println</span>(a &gt;= b) <span class="hljs-comment">// false</span>
<span class="hljs-title function_">println</span>(a &gt;= <span class="hljs-number">10</span>) <span class="hljs-comment">// true</span>
<span class="hljs-title function_">println</span>(a &lt;= b) <span class="hljs-comment">// true</span>
<span class="hljs-title function_">println</span>(b &lt;= <span class="hljs-number">20</span>) <span class="hljs-comment">// true</span>
</code></pre>
<h2 id="赋值运算符">赋值运算符</h2>
<p>以下假设变量a为10，变量b为20：</p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>描述</th>
<th>实例</th>
</tr>
</thead>
<tbody><tr>
<td>=</td>
<td>简单的赋值运算符</td>
<td>c = a + b 将 a + b 的运算结果赋值为 c</td>
</tr>
<tr>
<td>+=</td>
<td>加法赋值运算符</td>
<td>c += a 等效于 c = c + a</td>
</tr>
<tr>
<td>-=</td>
<td>减法赋值运算符</td>
<td>c -= a 等效于 c = c - a</td>
</tr>
<tr>
<td>*=</td>
<td>乘法赋值运算符</td>
<td>c *= a 等效于 c = c * a</td>
</tr>
<tr>
<td>/=</td>
<td>除法赋值运算符</td>
<td>c /= a 等效于 c = c / a</td>
</tr>
<tr>
<td>%=</td>
<td>取模赋值运算符</td>
<td>c %= a 等效于 c = c % a</td>
</tr>
</tbody></table>
<p>实例</p>
<pre><code class="hljs language-javascript">a = <span class="hljs-number">21</span>
b = <span class="hljs-number">10</span>
c = <span class="hljs-number">0</span>

c = a + b
<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;1 - c 的值为：&quot;</span>, c)

c += a
<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;2 - c 的值为：&quot;</span>, c)

c *= a
<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;3 - c 的值为：&quot;</span>, c)

c /= a
<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;4 - c 的值为：&quot;</span>, c)

c = <span class="hljs-number">2</span>
c %= a
<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;5 - c 的值为：&quot;</span>, c)
</code></pre>
